<template>
  <el-container style="min-height: 100vh">
    <el-aside :width="sideWidth + 'px'">
      <Aside :isCollapse="isCollapse" :logoTextShow="logoTextShow"></Aside>
    </el-aside>

    <el-container>
      <el-header style="border-bottom: 1px solid #ccc">
        <Header @collapse="collapse" :collapseBtnClass="collapseBtnClass" :user="user"></Header>
      </el-header>

      <el-main style="height: calc(100vh - 60px);overflow-y: auto ">
        <router-view @refreshUser="getUser" style="padding: 10px;background: rgb(2,0,36);
background: linear-gradient(45deg, rgba(2,0,36,0.6446953781512605) 0%, rgba(26,173,208,0.4654236694677871) 45%, rgba(0,219,255,0.5242471988795518) 100%);"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import Aside from "@/components/manage/Aside.vue";
import Header from "@/components/manage/Header.vue";

export default {
  name: "Manage",
  components: {
    Aside, Header
  },
  data() {
    return {
      isCollapse: false,
      logoTextShow: true,
      collapseBtnClass: 'el-icon-s-fold',
      sideWidth: 200,
      user: {},
    }
  },
  created() {
    this.getUser();
    this.getUserRole();
  },
  methods: {
    collapse() {
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) {
        // 收缩
        this.sideWidth = 48;
        this.logoTextShow = false;
        this.collapseBtnClass = 'el-icon-s-unfold';
      } else {
        this.sideWidth = 200;
        this.logoTextShow = true;
        this.collapseBtnClass = 'el-icon-s-fold';
      }
    },
    async getUser() {
      try {
        let id = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')).id : null;
        if (id) {
          const response = await this.request.get("/user/findById/" + id);
          if (response.code === '200') {
            this.user = response.data;
          } else {
            this.$message.error('找不到用户信息');
          }
        } else {
          this.$message.error('找不到用户信息');
        }
      } catch (error) {
        console.error('Error fetching user data:', error);
        this.$message.error('获取用户信息失败');
      }
    },
    async getUserRole() {
      try {
        const response = await this.request.get('/role/getRole');
        localStorage.setItem('role', JSON.stringify(response.data));
      } catch (error) {
        console.error('Error fetching user role:', error);
        this.$message.error('获取用户角色失败');
      }
    }
  }
}
</script>


<style scoped>
/*去掉aside侧边栏的底部滚动条*/
.el-aside::-webkit-scrollbar {
  display: none;

}
</style>